<template>
	<view class="zh-invoice-add">
		<u-form>
			<u-input title="开票名称" placeholder="填写公司名称" v-model="invoiceData.form.invoicetitle" labelWidth="160" :rules="invoiceData.rules.invoicetitle"></u-input>
			<u-input title="税号" placeholder="填写纳税人识别号" v-model="invoiceData.form.taxnumber" labelWidth="160" :rules="invoiceData.rules.taxnumber"></u-input>
			<u-input title="开户银行" placeholder="填写您的开户银行" v-model="invoiceData.form.bankname" labelWidth="160" :rules="invoiceData.rules.bankname"></u-input>
			<u-input title="银行账号" placeholder="填写您的银行账号" v-model="invoiceData.form.bankaccount" labelWidth="160" :rules="invoiceData.rules.bankaccount"></u-input>
			<u-input title="联系电话" placeholder="填写您的联系电话 " v-model="invoiceData.form.contactphone" labelWidth="160" :rules="invoiceData.rules.contactphone"></u-input>
			<!-- <u-select title="开票地址" placeholder="所在地区" @click="pickArea" :value="invoiceData.pickAreaStr" labelWidth="160" :rules="invoiceData.rules.address"></u-select> -->
			<u-input title="详细地址" placeholder="如道路、门牌号、小区、楼栋号、单元..." v-model="invoiceData.form.invoicingaddress" labelWidth="160" :rules="invoiceData.rules.invoicingaddress"></u-input>
			<cell-item title="设为默认" :border="false" :arrow="false">
				<template v-slot:right>
					<switch :checked="invoiceData.form.isdefault ==='1'" color="#267CF8" @change="changeDefault" style="transform:scale(0.7)"/>
				</template>
			</cell-item>
		</u-form>
		<view class="bottom-fixed">
			<button class="app-primary-button" hover-class="app-primary-button-active" @click="save">确定</button>
		</view>
		<!-- <picker-area :show="pickAreaData.show" @close="changePickAreaShow" @pickOne="pickOne"></picker-area> -->
	</view>
</template>

<script setup lang="ts">
	import { reactive } from 'vue'
	import { invoice } from '@/api'
	import { tip, util } from '@/utils'
	import type { InvoiceAddData } from '@/@type/invoice'
	import { InvoiceIsdefault, InvoiceStatus } from '@/enum/invoice'
	import { onLoad } from '@dcloudio/uni-app';
	import { useAuthStore } from '@/store/auth'
	
	const authStore = useAuthStore()
	const user = authStore.getUser()
	const invoiceData: any = reactive({
		rules: {
			invoicetitle:[{required:true,message:'请填写开票名称！'}],
			taxnumber:[{required:true,message:'请填写税号！'}],
			bankname:[{required:true,message:'请填写银行账号！'}],
			bankaccount:[{required:true,message:'请填写开户银行！'}],
			contactphone:[{required:true,message:'请填写联系电话！'}],
			invoicingaddress:[{required:true,message:'请填写详细地址！'}],
		},
		form: {			
			bankaccount: "",
			bankname: "",
			contactphone: "",
			createid: "",
			createname: "",
			createtime: "",
			id: "",
			invoicetitle: "",
			invoicingaddress: "",
			isdefault: InvoiceIsdefault.yes,
			status: InvoiceStatus.enable,
			taxnumber: "",
			updateid: "",
			updatename: "",
			updatetime: "",
			shopid: user.shopId,
			shopName:user.shopName
		},
		//pickAreaStr:'',
		//address:''
	})
	
	
	// const pickAreaData = reactive({
	// 	show:false
	// })
	
	onLoad((options) => {
		if(options.info) {
			const info = util.getObjParams(options.info)
			uni.setNavigationBarTitle({
				title:'修改开票资料'
			})
			invoiceData.form = info
		} else {
			uni.setNavigationBarTitle({
				title:'新增开票资料'
			})
		}
		
	})
	
	const invoiceAdd = () => {
		if(!invoiceData.form.invoicetitle) return tip.showToast('请输入开票名称')
		if(!invoiceData.form.taxnumber) return tip.showToast('请输入税号')
		if(!invoiceData.form.bankname) return tip.showToast('请输入开户银行')
		if(!invoiceData.form.bankaccount) return tip.showToast('请输入银行账号')
		if(!invoiceData.form.contactphone) return tip.showToast('请输入联系电话')
		//if(!invoiceData.pickAreaStr) return tip.showToast('请输入开票地址')
		if(!invoiceData.form.invoicingaddress) return tip.showToast('请输入详细地址')
		//invoiceData.form.invoicingaddress = invoiceData.pickAreaStr + invoiceData.address
		invoice.setInvoiceAdd(invoiceData.form).then((res: any) => {
			tip.showToast('保存成功！')
			uni.$emit('invoiceAdd')
			uni.navigateBack({delta:1})
		})
	}
	
	const invoiceEdit = () => {
		if(!invoiceData.form.invoicetitle) return tip.showToast('请输入开票名称')
		if(!invoiceData.form.taxnumber) return tip.showToast('请输入税号')
		if(!invoiceData.form.bankname) return tip.showToast('请输入开户银行')
		if(!invoiceData.form.bankaccount) return tip.showToast('请输入银行账号')
		if(!invoiceData.form.contactphone) return tip.showToast('请输入联系电话')
		//if(!invoiceData.pickAreaStr) return tip.showToast('请输入开票地址')
		if(!invoiceData.form.invoicingaddress) return tip.showToast('请输入详细地址')
		//invoiceData.form.invoicingaddress = invoiceData.pickAreaStr + invoiceData.address
		invoice.setInvoiceEdit(invoiceData.form).then((res: any) => {
			tip.showToast('修改成功！')
			uni.$emit('invoiceAdd')
			uni.navigateBack({delta:1})
		})
	}
	
	// const changePickAreaShow = () => {
	// 	pickAreaData.show = !pickAreaData.show
	// }
	
	// const pickArea = () => {
	// 	changePickAreaShow()
	// }
	
	// const pickOne = (data: any) => {
	// 	changePickAreaShow()
	// 	invoiceData.pickAreaStr = data[0] + data[1] + data[2]
	// }
	
	const changeDefault = (e: any) => {
		invoiceData.form.isdefault = e.detail.value?1:0
	}
	
	const save = () => {
		if(invoiceData.form.id) {
			invoiceEdit()
		} else {
			invoiceAdd()
		}
	}
</script>

<style lang="scss" scoped>
	.zh-invoice-add {

	}
</style>
